/**
 * Created by Administrator on 2018/7/4.
 */
//天气预报更新周期
let domain = "http://172.16.3.169:9999";//测试
// let domain = "http://172.16.3.169:9173";//正式
let version = "v1"; //接口版本
let xSign = "hlsofttech"; //系统签名-加密值 不同平台签名不同
let xToken = "zuo"; //用户认证信息-可以通过接口获取 暂时写死
let clientUrl = domain + "/api/" + version + "/common/interface/getByDataType";//接口地址 定量
let param = {"dataType": "a_a_a_a", "params": "time::20180731;;cbkCode::;;startIndex::1;;pageSize::10"};//接口请求参数 变量   新增用户数
let todayDate = '';
let yesterdayDate = '';
let beforeYesterdayDate = '';
let beforeYesterdayDateWithHyphen = '';
let hasGetWeather = false;
let getWeatherPeriod = 1;
let bodyScale = 1;

// 自定义方法
(function initTools() {
    $.fn.extend({
        /**
         * 将文本转为数字并保留相应小数位数
         * @param n 小数位数
         * @param power 数据缩放到10的多少次方
         * @param str 后面可以跟上个字符串，比如‘%’
         */
        str2NumFixed: function (n, power, str = '') {
            $.each($(this), function () {
                $(this).text(hasVal(parseFloat($(this).text() + 'e' + power).toFixed(n) + str));
            })
        }
    })
})();

function hasVal(val) {
    if (val === null) {
        return '-';
    }
    return val;
}

/**
 * 通过时间对象获取日期和时间格式
 * @param dateObj 时间对象
 * @return {{timeStr: string, dateStr: string, dateStrNoHyphen: string}}
 * @constructor
 */
function DateFormatter(dateObj) {
    let [year, mon, date, hour, min, sec, milliSec] = [
        dateObj.getFullYear(),
        dateObj.getMonth() + 1,
        dateObj.getDate(),
        dateObj.getHours(),
        dateObj.getMinutes(),
        dateObj.getSeconds(),
        dateObj.getMilliseconds()
    ];
    let timeStr = `${hour}:${min > 9 ? min : "0" + min}:${sec > 9 ? sec : "0" + sec}`;
    let dateStr = `${year}-${mon > 9 ? mon : "0" + mon}-${date > 9 ? date : "0" + date}`;
    let dateStrNoHyphen = `${year}${mon > 9 ? mon : "0" + mon}${date > 9 ? date : "0" + date}`;
    let dateStrWithHyphen = `${year}-${mon > 9 ? mon : "0" + mon}-${date > 9 ? date : "0" + date}`;
    return {timeStr, dateStr, dateStrNoHyphen, dateStrWithHyphen, year, mon, date}
}

function pageResize() {
    [pageH, pageW] = [$(window).height(), $(window).width()];
    if (pageW / pageH > 16 / 9) { //扁
        $("#container").css({width: pageH * 16 / 9, height: '100%'});
        bodyScale = pageH / 1080;
    } else { //方
        $("#container").css({height: pageW * 9 / 16, width: '100%'});
        bodyScale = pageW / 1920;
    }
    $("html").css("font-size", bodyScale * 16 + "px").css("opacity", 1);
    //遍历图表自动缩放
    if (typeof(charts) !== 'undefined' && (Object.keys(charts).length !== 0)) {
        for (let chartKey of Object.keys(charts)) {
            charts[chartKey].resize()
        }
    }
}

onresize = function () {
    pageResize();
    pageResize();

};

//设置请求header
function setHeader(request) {
    request.setRequestHeader("X-Sign", xSign);
    request.setRequestHeader("X-Token", xToken);
}

//获取token
function getToken(loadData) {
    $.post({
        url: domain + '/api/' + version + '/accessToken',
        data: {
            loginName: 'admin',
            password: '123456'
        },
        dataType: 'json',
        beforeSend: function (request) {
            request.setRequestHeader("Accept", '*/*');
            request.setRequestHeader("X-Platform", "PC");
        },
        success: function (data) {
            if (data.data.accessToken) {
                xToken = data.data.accessToken;
            }
            loadData();
        },
        error: function () {
            loadData();
        }
    });
}

//哈萨克斯坦的国家有两条数据，其中一条写上后整个国家就不能被鼠标移上时有响应。剪切出来保存到这里
/*let Kazakhstan = {
    "type": "Feature",
    "properties": {
        "name": "哈萨克斯坦",
        "scalerank": 0,
        "featurecla": "Admin-0 country",
        "LABELRANK": 5,
        "SOVEREIGNT": "Kazakhstan",
        "SOV_A3": "KAZ",
        "ADM0_DIF": 1,
        "LEVEL": 2,
        "TYPE": "Lease",
        "ADMIN": "Baykonur Cosmodrome",
        "ADM0_A3": "KAB",
        "GEOU_DIF": 0,
        "GEOUNIT": "Baykonur Cosmodrome",
        "GU_A3": "KAB",
        "SU_DIF": 0,
        "SUBUNIT": "Baykonur Cosmodrome",
        "SU_A3": "KAB",
        "BRK_DIFF": 1,
        "NAME": "Baikonur",
        "NAME_LONG": "Baikonur Cosmodrome",
        "BRK_A3": "B40",
        "BRK_NAME": "Baykonur",
        "BRK_GROUP": null,
        "ABBREV": "Bayk.",
        "POSTAL": "BK",
        "FORMAL_EN": null,
        "FORMAL_FR": null,
        "NOTE_ADM0": "Leased by Russia",
        "NOTE_BRK": "Leased to Russia by Kazakhstan",
        "NAME_SORT": "Baikonur Cosmodrome",
        "NAME_ALT": "Baykonur Cosmodrome",
        "MAPCOLOR7": 6,
        "MAPCOLOR8": 1,
        "MAPCOLOR9": 6,
        "MAPCOLOR13": 1,
        "POP_EST": 70000,
        "GDP_MD_EST": 1155,
        "POP_YEAR": -99,
        "LASTCENSUS": -99,
        "GDP_YEAR": -99,
        "ECONOMY": "2. Developed region: nonG7",
        "INCOME_GRP": "3. Upper middle income",
        "WIKIPEDIA": 0,
        "FIPS_10_": "-99",
        "ISO_A2": "-99",
        "ISO_A3": "-99",
        "ISO_N3": "-99",
        "UN_A3": "-099",
        "WB_A2": "-99",
        "WB_A3": "-99",
        "WOE_ID": -90,
        "WOE_ID_EH": 20070177,
        "WOE_NOTE": "A subunit of Kazakhstan in WOE.",
        "ADM0_A3_IS": "KAZ",
        "ADM0_A3_US": "KAB",
        "ADM0_A3_UN": -99,
        "ADM0_A3_WB": -99,
        "CONTINENT": "Asia",
        "REGION_UN": "Asia",
        "SUBREGION": "Central Asia",
        "REGION_WB": "Europe & Central Asia",
        "NAME_LEN": 8,
        "LONG_LEN": 19,
        "ABBREV_LEN": 5,
        "TINY": -99,
        "HOMEPART": -99
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [[[63.44354821800022, 45.567969055000134], [63.38391361500021, 45.56582448300014], [63.32427901200009, 45.567969055000134], [63.26634973200004, 45.574221904000126], [63.2104875080002, 45.58437632300007], [63.15689904800013, 45.59825144500016], [63.105946086000074, 45.61561472600016], [63.05783532700008, 45.63628529900018], [63.0129285070002, 45.66000478100014], [62.971484009000164, 45.68664398300007], [62.93386356600013, 45.71594451900012], [62.90027388500016, 45.747699687000164], [62.871025025000165, 45.7817027800001], [62.846478719000174, 45.8177729300001], [62.82689335100014, 45.855703430000204], [62.81252730300025, 45.89518422500004], [62.80363895700003, 45.93616363500007], [62.800641723000126, 45.97833160400012], [62.80363895700003, 46.020499573000166], [62.81252730300025, 46.06142730700009], [62.82689335100014, 46.10098561600006], [62.846478719000174, 46.138890280000055], [62.871025025000165, 46.17496042900015], [62.90027388500016, 46.20896352200009], [62.93386356600013, 46.24071868900012], [62.971484009000164, 46.270019227000176], [63.0129285070002, 46.29665842700021], [63.05783532700008, 46.32037790900006], [63.105946086000074, 46.341048483000066], [63.15689904800013, 46.358411764000095], [63.2104875080002, 46.37226104700005], [63.26634973200004, 46.38244130500013], [63.32427901200009, 46.388694153000216], [63.38391361500021, 46.390838725000094], [63.44354821800022, 46.388694153000216], [63.50147749900003, 46.38244130500013], [63.55739139800005, 46.37226104700005], [63.61092818200018, 46.358411764000095], [63.66188114500011, 46.341048483000066], [63.709940226000185, 46.32037790900006], [63.75489872300011, 46.29665842700021], [63.79634322100026, 46.270019227000176], [63.83396366400018, 46.24071868900012], [63.86755334500003, 46.20896352200009], [63.8966988520001, 46.17496042900015], [63.92129683500019, 46.138890280000055], [63.94093387900017, 46.10098561600006], [63.95529992700017, 46.06142730700009], [63.96408492000023, 46.020499573000166], [63.96713383000008, 45.97833160400012], [63.96408492000023, 45.93616363500007], [63.95529992700017, 45.89518422500004], [63.94093387900017, 45.855703430000204], [63.92129683500019, 45.8177729300001], [63.8966988520001, 45.7817027800001], [63.86755334500003, 45.747699687000164], [63.83396366400018, 45.71594451900012], [63.79634322100026, 45.68664398300007], [63.75489872300011, 45.66000478100014], [63.709940226000185, 45.63628529900018], [63.66188114500011, 45.61561472600016], [63.61092818200018, 45.59825144500016], [63.55739139800005, 45.58437632300007], [63.50147749800004, 45.574221904000126], [63.44354821800022, 45.567969055000134]]]
    }
};*/
